<template>
    <div>
        <h3>房型添加</h3>
        <table class="table table-bordered">
            <tbody>
                <tr>
                    <td>房型名称</td>
                    <td><input type="text" v-model="a.Name"></td>
                </tr>
                <tr>
                    <td>图片</td>
                    <td><input type="file" @change="imgs">
                        <img :src="a.Photo" width="200">
                    </td>
                </tr>
                <tr>
                    <td>面积</td>
                    <td><input type="text" v-model="a.Area"></td>
                </tr>
                <tr>
                    <td>状态</td>
                    <td><input type="radio" v-model="a.State" name="state" :value="1">已投放
                        <input type="radio" v-model="a.State" name="state" :value="0">未投放
                    </td>
                </tr>
                <tr>
                    <td></td>
                    <td><input type="button" value="添加" @click="add"></td>
                </tr>
            </tbody>
        </table>

    </div>
</template>

<script setup lang="ts">
import axios from 'axios';
import { ref, reactive, onMounted } from 'vue'
import { useRoute, useRouter } from 'vue-router';
const route = useRoute();
const router = useRouter();

let a: any = reactive({
    Name: '',
    Photo: '',
    Area: '',
    State: 1
})


const imgs: any = (e: any) => {
    let obj = e.target.files[0]
    let f = new FormData()
    f.append('file', obj)
    axios({
        method: 'post',
        url: '/api/Img/UpImg',
        data: f
    })
        .then((res) => {
            if (res.data == "图片格式错误") {
                alert("图片格式错误");
                return;
            }
            if (res.data == "图片大于2MB") {
                alert("图片大于2MB");
                return;
            }
                a.Photo = res.data
                alert("上传成功");
        })
}

const add:any=()=>{
    axios({
        method:'post',
        url:'/api/House/Add',
        data:a
    })
    .then((res) => {
            if (res.data == -1) {
                alert("名称重复");
                return;
            }
            if (res.data > 0) {
                alert("添加成功");
                router.push("/show")
            }
        })
}

</script>

<style scoped></style>